﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using TO3.Models;
using TO3.DAL;

namespace TO3.Controllers
{
    public class RankingsController : Controller
    {
        private TournamentContext db = new TournamentContext();
        int tournamentId;

        // GET: /Rankings/
        public ActionResult Index()
        {
            //Get the active tournamentId (if any)
            if (Session["ActiveTournamentId"] == null)
            {
                this.tournamentId = 0;
            }
            else
            {
                this.tournamentId = (int)Session["ActiveTournamentId"];
                //Get the tournament name            
                ViewBag.TournamentName = db.Tournaments.Single(t => t.Id == tournamentId).Name;
            }            

            //Get the rankings
                        
            Random rand = new Random();

            var TournamentRegistrations = from registrations in db.TournamentRegistrations
                                          where registrations.TournamentId == this.tournamentId
                                          select registrations.PlayerId;

            var playersInTournament = (from player in db.Players
                                       where TournamentRegistrations.Contains(player.Id) && player.Name != "Organiser/Freebie"
                                       select player).ToList();

            var matchesInTournament = db.Matches.Where(m => m.TournamentId == this.tournamentId);

            var sortedPlayerScoreList = (from p in playersInTournament
                                         join m in matchesInTournament on p.Id equals m.Player1Id into pm1
                                         join m2 in matchesInTournament on p.Id equals m2.Player2Id into pm2
                                         join m3 in matchesInTournament on p.Id equals m3.WinnerId into pm3

                                         select new { id = p.Id, Name = p.Name, TotalCP = pm1.Sum(m => m.Player1CP) + pm2.Sum(m2 => m2.Player2CP), Wins = pm3.Count() } into selection
                                         orderby selection.Wins descending, selection.TotalCP descending, rand.Next(1000)
                                         select selection
                                             ).ToList();

            List<RankingsViewModel> rankings = new List<RankingsViewModel>();
            RankingsViewModel playerRanking;

            foreach (var item in sortedPlayerScoreList)
            {
                playerRanking = new RankingsViewModel();
                playerRanking.Id = item.id;
                playerRanking.Name = item.Name;
                playerRanking.TotalCP = item.TotalCP;
                playerRanking.Wins = item.Wins;

                rankings.Add(playerRanking);
            }

            return View(rankings);
        }
    }
}
